/**
 * Created by WT on 2017/3/14.
 */

var isSearchBarExpand = false;
var index;

/**
 * 关闭layer
 */
function closeLayer() {
    layer.close(index);
}
/**
 * 使用layer 打开一个页面
 * @param title
 * @param content
 */
function openLayer(title,content) {

    index=layer.open({
        type: 2,
        title: title,
        resize:false,
       // maxmin: true,
        area: ['600px', '60%'],
        content: ROOT_PATH+'/permission/duty/'+content
    });
}



$(function () {
    //加载表格
    // sort, addUserId, addTime, updateUserId, updateTime
    $('#dutyList').datagrid({
        method:'post',
        iconCls: 'icon-edit',//图标
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect:true,
        fitColumns: true,
        pagination: true,
        url:ROOT_PATH+'/permission/duty/list.shtml',
        queryParams:{
            sort:'sort',
            order:'asc'
        },
        columns:[[
            {field: 'id',checkbox: true},
            {field:'code',title:'职位编号',width:100,sortable:true},
            {field:'name',title:'职位名称',width:100,sortable:true},
            {field:'addTime',title:'添加时间',width:100,sortable:true},
            {field:'sort',title:'排序号',width:100,sortable:true},
            {field:'remarks',title:'备注',width:100,sortable:true}
            /*
             {field:'updateTime',title:'修改时间',width:100,sortable:true},
            {field:'useState',title:'启用状态',width:100,formatter: function (value, row, index) {
                return row.useState == '0' ? '启用' : '未启用';
            }}*/
        ]],
        toolbar:'#toolbar'
    });

    //添加按钮事件
    $('#btn_duty_add').on('click',function () {

        var title='添加职位';
        var content='add.shtml';
        openLayer(title,content);
    });
    //修改按钮事件
    $('#btn_duty_edit').on('click',function () {

        var rows=$('#dutyList').datagrid('getChecked');

        if(rows.length != 1){
            layer.alert("请选择需要修改的职位！");
            return
        }

        var id=rows[0].id;
        var title='修改职位';
       var content='edit.shtml?id='+id;
       openLayer(title,content);
    });
    //删除按钮事件
    $('#btn_duty_delete').on('click',function () {

        var rows = $('#dutyList').datagrid('getSelections');

        if(rows.length != 1){
            layer.alert("请选择需要删除的职位！");
            return
        }
        var id=rows[0].id;
        layer.confirm('是否确定删除当前选中数据？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type:'post',
                url : ROOT_PATH+'/permission/duty/deleteById.shtml',
                data :{id:id},
                dataType:"json",
                success: function (result) {
                    if (result.success) {
                        $('#dutyList').datagrid('clearSelections').datagrid('load');
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000
                        });
                    }
                    else {
                        layer.msg('删除失败', {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            });
        });

    });

    //查询按钮
    $('#searchDuty').on('click',function(){
        var queryParams=$('#headForm').serializeJson();
        queryParams.sort='sort';
        queryParams.order='asc';
        $('#dutyList').datagrid({
            queryParams:queryParams
        });
    });
    //清空查询条件
    $('#clearSerch').on('click',function(){
       $('#headForm').form('reset');
    })

});





